// import { Message } from "uview-ui";
import { smsLogin } from '../api/login'
// import WebIM from '../utils/WebIM'
let WebIM = require("../utils/WebIM")["default"];


const Login = {
    state: {
        isRegister: false,
        username: "",
        userDetail: {},
        verificationKey: false
    },
    mutations: {
        setUserName(state, username) {
            state.username = username;
        },
        setRegisterFlag(state, flag) {
            state.isRegister = flag;
        },
        setUserDetaild(state, user_detail) {
            state.userDetail = user_detail
        }
    },
    actions: {
        onLogin({
            commit
        }, payload) {
            return new Promise((resolve, reject) => {
                smsLogin(payload).then(res => {
                        resolve()
                    })
                    .catch(reject)
            })
        },
        onLogin: function(context, payload) {
            // context.commit("setUserName", payload.username);
            var options = {
                user: payload.username,
                pwd: payload.password,
                appKey: WebIM.config.appkey,
                apiUrl: 'https://a1.easecdn.com'
            };
            getApp().globalData.conn.open(options);
            localStorage.setItem("userInfo", JSON.stringify({ userId: payload.username, password: payload.password }));
            uni.setStorageSync("myUsername", payload.username)
        },
        onLogout: function(context) {
            context.commit("setUserName", "");
            localStorage.setItem("userInfo", "");
            WebIM.conn.close();
        },
        onRegister: function(context, payload) {
            const _this = this;
            // context.commit('setUserName', payload.username)
            var options = {
                username: payload.username,
                password: payload.password,
                nickname: payload.nickname,
                appKey: WebIM.config.appkey,
                success: () => {
                    // uni.showToast("注册成功")
                    context.commit("setRegisterFlag", false)
                    context.dispatch('onLogin', payload)
                },
                error: (err) => {
                    console.log(err.data.error);
                    if (err.data.error == "duplicate_unique_property_exists") {
                        // uni.showToast("用户已存在！")
                        context.dispatch('onLogin', payload)

                    } else if (err.data.error == "illegal_argument") {
                        if (err.data.error_description === 'USERNAME_TOO_LONG') {
                            // return uni.showToast('用户名超过64个字节！')
                        }
                        // uni.showToast("用户名不合法！")
                    } else if (err.data.error == "unauthorized") {
                        // uni.showToast("注册失败，无权限！")
                    } else if (err.data.error == "resource_limited") {
                        // uni.showToast("您的App用户注册数量已达上限,请升级至企业版！")
                    }
                }
            };
            WebIM.conn.registerUser(options);
        },
        getLoginUserInfo: (context, payload) => {
            const { userId } = payload
            WebIM.conn.fetchUserInfoById(userId).then((res) => {
                console.log(res.data[userId])
                let user_detail = res.data[userId];
                res.data[userId] && context.commit('setUserDetaild', user_detail)
            })
        },
        updateOwnUserInfo: ({ commit }, payload) => {
            const { infoValue, type } = payload;
            WebIM.conn.updateOwnUserInfo(type, infoValue).then((res) => {
                res.data && commit('setUserDetaild', res.data)
            })
        },
        setRegisterFlag: function(context, flag) {
            const path = flag ? "/register" : "/login";
            Vue.$router.push(path);
            context.commit("setRegisterFlag", flag);
        }


    },
    getters: {

    }
};
export default Login;